﻿using BCCommon;
using BCData.Common.Dictionary;
using BCData.OA.TeamWorks.TaskRecord;
using BCDto.OA.Workflow.WorkflowActivityInstance;
using BCDto.OA.Workflow.WorkflowOperation;
using BCEntity.Common.Dictionary;
using BCEntity.OA.TeamWorks.TaskRecord;
using BCService.OA.Workflow.WorkflowActivityInstanceBusiness;
using System;

namespace BCService.OA.Workflow.WorkflowFactoryInstance
{
    /// <summary>
    /// OA工作流工厂实例
    /// </summary>
    public class OAWorkflowFactoryInstanceService : IOAWorkflowFactoryInstanceService
    {
        private readonly IWorkflowActivityInstanceBusinessService workflowActivityInstanceBusinessService;
        private readonly IDictionaryReferenceData dictionaryReferenceData;
        private readonly ITaskRecordData taskRecordData;
        private readonly IRedisService redisService;
        private readonly IApplicationContextService applicationContextService;

        public OAWorkflowFactoryInstanceService(IWorkflowActivityInstanceBusinessService workflowActivityInstanceBusinessService,
            IDictionaryReferenceData dictionaryReferenceData,
            ITaskRecordData taskRecordData,
            IRedisService redisService,
            IApplicationContextService applicationContextService)
        {
            this.workflowActivityInstanceBusinessService = workflowActivityInstanceBusinessService;
            this.dictionaryReferenceData = dictionaryReferenceData;
            this.taskRecordData = taskRecordData;
            this.redisService = redisService;
            this.applicationContextService = applicationContextService;
        }
        /// <summary>
        /// 关闭任务
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Closed(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IOABusinessFactoryService oABusinessFactoryService = this.GetAssignService(instanceDto);
            oABusinessFactoryService.Closed(instanceDto, taskRecordEntity, workflowOperationRequestDto); 
        }

        /// <summary>
        /// 初始化活动步骤实例
        /// </summary>
        /// <param name="activityInstanceId"></param>
        /// <returns></returns>
        public bool InitActivityStepInstanceAssign(long activityInstanceId)
        {
            var instanceDto = GetActivityInstanceEntity(activityInstanceId);
            IOABusinessFactoryService oABusinessFactoryService = this.GetAssignService(instanceDto);
            return oABusinessFactoryService.InitActivityStepInstanceAssign(instanceDto);
        }

        /// <summary>
        /// 初始化指派
        /// </summary>
        /// <param name="workflowActivityInstanceDto"></param>
        /// <returns></returns>
        public bool InitAssign(WorkflowActivityInstanceDto workflowActivityInstanceDto)
        {
            IOABusinessFactoryService oABusinessFactoryService = this.GetAssignService(workflowActivityInstanceDto);
            return oABusinessFactoryService.InitAssign(workflowActivityInstanceDto);
        }

        /// <summary>
        /// 指派
        /// </summary>
        /// <param name="workflowActivityInstanceDto"></param>
        /// <returns></returns>
        private IOABusinessFactoryService GetAssignService(WorkflowActivityInstanceDto workflowActivityInstanceDto)
        {
            DictionaryReferenceEntity dictionaryReferenceEntity = this.redisService.TryGet(workflowActivityInstanceDto.WorkflowKey, () =>
            {
                return this.dictionaryReferenceData.GetEntityAsync(workflowActivityInstanceDto.WorkflowKey).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));

            if (dictionaryReferenceEntity == null)
            {
                throw new ArgumentException("字典中缺少工作流Key，工作流Key:" + workflowActivityInstanceDto.WorkflowKey);
            }

            IOABusinessFactoryService oABusinessFactoryService = null;
            //switch (dictionaryReferenceEntity.TypeName)
            //{
            //    //考勤补卡
            //    case "RemedyCheckIn":
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IAttendanceBusinessFactoryService>();
            //        break;
            //    case "VehicleDetain"://扣押车辆
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IVehicleDetainBusinessService>();
            //        break;
            //    case "VehicleAllot"://车辆调拨
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IVehicleAllotBusinessService>();
            //        break;
            //    case "TerminateContract"://终止合同
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IVehicleTerminateContractWorkflowService>();
            //        break;
            //    case "Probation"://转正
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IProbationWorkflowService>();
            //        break;
            //    case "Raises"://加薪
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IRaisesWorkflowService>();
            //        break;
            //    case "VehicleSaleAllot"://车辆调拨
            //        oABusinessFactoryService = applicationContextService.RequestServices.GetService<IVehicleSaleAllotBusinessService>();
            //        break;
            //    default:
            //        oABusinessFactoryService = null;
            //        break;
            //}
            return oABusinessFactoryService;
        }

        /// <summary>
        /// 通过
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Pass(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IOABusinessFactoryService oABusinessFactoryService = this.GetAssignService(instanceDto);
            oABusinessFactoryService.Pass(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        /// <summary>
        /// 挂起
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Paused(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IOABusinessFactoryService oABusinessFactoryService  = this.GetAssignService(instanceDto);
            oABusinessFactoryService.Paused(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        /// <summary>
        /// 驳回
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Reject(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IOABusinessFactoryService oABusinessFactoryService = this.GetAssignService(instanceDto);
            oABusinessFactoryService.Reject(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        #region 获取活动实例信息
        /// <summary>
        /// 获取活动实例信息(带步骤实例列表)
        /// </summary>
        /// <param name="activityInstanceId">活动实例Id</param>
        /// <returns></returns>
        private WorkflowActivityInstanceDto GetActivityInstanceEntity(long activityInstanceId)
        {
            return this.workflowActivityInstanceBusinessService.GetInstanceInfo(activityInstanceId);
        }
        #endregion

        #region 获取任务记录信息
        private TaskRecordEntity GetTaskRecordEntity(string taskRecordNo)
        {
            var taskRecordEntity = this.taskRecordData.GetEntity(taskRecordNo).GetAwaiter().GetResult();
            if (taskRecordEntity == null)
            {
                throw new ArgumentException("此任务编号不存在");
            }
            return taskRecordEntity;
        }
        #endregion
    }
}
